namespace Brjnk.GpxExplorer
{
    using System.Windows;
    using Brjnk.Common;

    /// <summary>
    /// </summary>
    public class BoundsBuilder
    {
        private double minX = 0;
        private double maxX = 0;
        private double minY = 0;
        private double maxY = 0;

        private bool first = true;

        public virtual void AddPoint(GpsCoordinates coordinates)
        {
            AddPoint(coordinates.LonTotalDegree, coordinates.LatTotalDegree);
        }

        public bool IsEmpty { get { return first; } }

        public void Add(Bounds bounds)
        {
            AddPoint(bounds.Min);
            AddPoint(bounds.Max);
            AddPoint(new Point(bounds.Min.X, bounds.Max.Y));
            AddPoint(new Point(bounds.Max.X, bounds.Min.Y));
        }

        public virtual void AddPoint(double x, double y)
        {
            if (first)
            {
                minX = x;
                maxX = x;
                minY = y;
                maxY = y;
                first = false;
            }
            else
            {
                if (x < minX) minX = x;
                if (y < minY) minY = y;
                if (x > maxX) maxX = x;
                if (y > maxY) maxY = y;
            }
        }

        public Bounds GetBounds()
        {
            return new Bounds(new Point(minX, minY), new Point(maxX, maxY));
        }

        public void AddPoint(Point point)
        {
            AddPoint(point.X, point.Y);
        }
    }

    public struct Bounds
    {
        public Bounds(Point min, Point max)
            : this()
        {
            Min = min;
            Max = max;
        }

        public Bounds(Point point)
            : this(point, point)
        {
        }

        public Point Min { get; private set; }

        public Point Max { get; private set; }

        public bool IsOnePoint { get { return Min == Max; } }
    }
}